package com.example.wordtris;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.example.wordtris.WTBackground;
import com.example.wordtris.WTGameVars;
import com.example.wordtris.WTTextures;
import com.example.wordtris.WTPlayer;






import android.opengl.GLSurfaceView.Renderer;

public class WTGameRenderer implements Renderer{

	private WTBackground background = new WTBackground();
	private WTPlayer player1 = new WTPlayer();
	private WTLetter letter = new WTLetter();
	private WTTextures textureLoader;
	private long loopStart = 0;
	private long loopEnd = 0;
	private long loopRunTime = 0 ;
	private int[] spriteSheets = new int[3];
	
	@Override
	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		loopStart = System.currentTimeMillis();
		// TODO Auto-generated method stub
		try {
			if (loopRunTime < WTGameVars.GAME_THREAD_FPS_SLEEP){
				Thread.sleep(WTGameVars.GAME_THREAD_FPS_SLEEP - loopRunTime);
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);	
		
		drawBackground1(gl);
		drawPlayer1(gl);
		moveLetter(gl);
		//detectCollisions(gl);
	    loopEnd = System.currentTimeMillis();
	    loopRunTime = ((loopEnd - loopStart));
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		gl.glViewport(0, 0, width,height);
		 
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		
		gl.glOrthof(0f, 1f, 0f, 1f, -1f, 1f);
		
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
		// TODO Auto-generated method stub
	//	initializeBricks();
		textureLoader = new WTTextures(gl);
		spriteSheets = textureLoader.loadTexture(gl, WTGameVars.BASE, WTGameVars.context, 1); 
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);

		background.loadTexture(gl,WTGameVars.BACKGROUND, WTGameVars.context);
		//player1.loadTexture(gl,WTGameVars.BASE, WTGameVars.context);
		letter.loadTexture(gl, WTGameVars.LETTERA, WTGameVars.context);
	}
	
	private void drawBackground1(GL10 gl){
		
	    gl.glMatrixMode(GL10.GL_MODELVIEW);
	    gl.glLoadIdentity();
	    gl.glPushMatrix();
	    gl.glScalef(1f, 1f, 1f);
	    //gl.glTranslatef(0f, 0f, 0f);
	   
	    background.draw(gl);
	    gl.glPopMatrix();
	    gl.glLoadIdentity();


	}
	private void drawPlayer1(GL10 gl){

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		gl.glPushMatrix();
		gl.glScalef(.90f, .30f, 0f);		
		player1.draw(gl,spriteSheets);
		gl.glPopMatrix();
		gl.glLoadIdentity();
		
		//player1.PosY=

}
	
	private void moveLetter(GL10 gl){

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		gl.glPushMatrix();
		gl.glScalef(.25f, .25f, 1f);	
		if (WTGameVars.playerAction == WTGameVars.LETTER_MOVE_LEFT_1 && WTGameVars.playerBankPosX > 0)
		{
			WTGameVars.playerBankPosX = WTGameVars.playerBankPosX - WTGameVars.LETTER_MOVE_SPEED;
			
		}
		else if(WTGameVars.playerAction == WTGameVars.LETTER_MOVE_RIGHT_1 &&  WTGameVars.playerBankPosX < 3.7)
		{
			WTGameVars.playerBankPosX = WTGameVars.playerBankPosX + WTGameVars.LETTER_MOVE_SPEED;
		}
		//letter.posX += (float) ((WTGameVars.letterTargetX - letter.posX )/ (letter.posY / (WTGameVars.letterTargetY )));

		letter.posY -= WTGameVars.letterTargetY;// * 3;
		gl.glTranslatef(WTGameVars.playerBankPosX,letter.posY,0f);//WTGameVars.playerBankPosY, 0f);
		gl.glMatrixMode(GL10.GL_TEXTURE);
		gl.glLoadIdentity();
		gl.glTranslatef(0.0f,0.0f, 0.0f);


		letter.draw(gl);
		gl.glPopMatrix();
		gl.glLoadIdentity();

}
	
	private void detectCollisions(GL10 gl){
	
		if(letter.posY == WTGameVars.playerBankPosY){
			letter.posY=0f;
			moveLetter(gl);
			}
		}

}
